const userService = require('../service/user-service')
const validation = require('../middleware/validation')
const Joi = require('joi')

class UserControllers {
  async login (ctx) {
    try {
      ctx.body = await userService.signIn(ctx.request.body)
    } catch (error) {
      return Promise.reject(error)
    }
  }
  
  async getUserInfo (ctx) {
    try {
      ctx.body = await userService.getUserInfo(ctx.state.user)
    } catch (error) {
      return Promise.reject(error)
    }
  }

  @validation.validateRoute({
    account: Joi.string().min(3).max(30).required(),
    password: Joi.string().min(6).required(),
  })
  async createUser (ctx) {
    try {
      ctx.body = await userService.createUser(ctx.request.body)
    } catch (error) {
      return Promise.reject(error)
    }
  }
}

module.exports = new UserControllers()